
### APPENDIX C2: OLS with Wild bootstrap SE

# load, combine data
library(rio)
library(tidyverse)

# estimate models
library(brglm2)

# cluster standard errors
library(lmtest)
library(sandwich)

# generate tables
library(stargazer)




# LOAD DATA
glm_df <- import("glm_data.csv") %>%
  filter(iso3c!="TTO" & iso3c!="GUY" & iso3c!="SUR") %>%
  filter(!is.na(approval) & !is.na(opp1vote) & !is.na(number_protests))



## TABLE C3: ALL DOCS
brglm1 <- lm(doc ~ approval + opp1vote + number_protests +
             as.factor(iso3c) + as.factor(quarter) + as.factor(year), 
             data = glm_df)

brglm1_boot <- brglm1 %>%
  coeftest(vcovBS(brglm1, type = "wild", cluster =  ~ iso3c))



brglm2 <- lm(doc ~ previous_doc + duck + partyage + polity2 + left_exec + any_election + minister_is_technocrat_mainstream +
             as.factor(iso3c) + as.factor(quarter) + as.factor(year), 
             data = glm_df)

brglm2_boot <- brglm2 %>%
  coeftest(vcovBS(brglm2, type = "wild", cluster =  ~ iso3c))



brglm3 <- lm(doc ~ resource_rents_lag + discovery + real_oil_price_log + gdp_pc_constant_lag + gdp_growth_lag + imf_program + crisis +
             as.factor(iso3c) + as.factor(quarter) + as.factor(year), 
             data = glm_df)

brglm3_boot <- brglm3 %>%
  coeftest(vcovBS(brglm3, type = "wild", cluster =  ~ iso3c))



brglm4 <- lm(doc ~ approval + opp1vote + number_protests +
              previous_doc + duck + partyage + polity2 + left_exec + any_election + minister_is_technocrat_mainstream +
              as.factor(iso3c) + as.factor(quarter) + as.factor(year), 
              data = glm_df)

brglm4_boot <- brglm4 %>%
  coeftest(vcovBS(brglm4, type = "wild", cluster =  ~ iso3c))



brglm5 <- lm(doc ~ approval + opp1vote + number_protests + 
              resource_rents_lag + discovery + real_oil_price_log + gdp_pc_constant_lag + gdp_growth_lag + imf_program + crisis +
              as.factor(iso3c) + as.factor(quarter) + as.factor(year), 
              data = glm_df)

brglm5_boot <- brglm5 %>%
  coeftest(vcovBS(brglm5, type = "wild", cluster =  ~ iso3c))



brglm6 <- lm(doc ~ approval + opp1vote + number_protests + previous_doc + duck + partyage + polity2 + left_exec + any_election +
              minister_is_technocrat_mainstream +
              resource_rents_lag + discovery + real_oil_price_log + gdp_pc_constant_lag + gdp_growth_lag + imf_program + crisis +
              as.factor(iso3c) + as.factor(quarter) + as.factor(year), 
              data = glm_df)

brglm6_boot <- brglm6 %>%
  coeftest(vcovBS(brglm6, type = "wild", cluster =  ~ iso3c))



# generate tables
stargazer(brglm1_boot,brglm2_boot,brglm3_boot,brglm4_boot,brglm5_boot,brglm6_boot, #type="text",
          omit=c("iso3c","quarter","year"), no.space = T,
          covariate.labels = c("Executive approval (perc)","Opposition vote share (perc)","Number of protests",
                               "Previous document = 1","Lame duck = 1", "Party age",
                               "Democracy","Left executive = 1","Election quarter = 1",
                               "Mainstream minister = 1",
                               "Resource rents (perc)","Field discovery = 1","Oil price (USD, log)",
                               "GDP per capita (1,000 USD)","GDP growth (perc)","IMF program = 1", "Crisis = 1"), 
          dep.var.labels = "Any document")

# retrieve n, r2, etc
stargazer(brglm1,brglm2,brglm3,brglm4,brglm5,brglm6, #type="text",
          dep.var.labels = "Any document")


## TABLE C4: FUNDS
brglm7 <- lm(fund_doc ~ approval + opp1vote + number_protests +
              as.factor(iso3c) + as.factor(quarter) + as.factor(year), 
              data = glm_df)

brglm7_boot <- brglm7 %>%
  coeftest(vcovBS(brglm7, type = "wild", cluster =  ~ iso3c))



brglm8 <- lm(fund_doc ~ previous_fund_doc + duck + partyage + polity2 + left_exec + any_election + minister_is_technocrat_mainstream +
                        as.factor(iso3c) + as.factor(quarter) + as.factor(year), 
                        data = glm_df)

brglm8_boot <- brglm8 %>%
  coeftest(vcovBS(brglm8, type = "wild", cluster =  ~ iso3c))



brglm9 <- lm(fund_doc ~ resource_rents_lag + discovery + real_oil_price_log + gdp_pc_constant_lag + gdp_growth_lag + imf_program + crisis +
              as.factor(iso3c) + as.factor(quarter) + as.factor(year), 
              data = glm_df)

brglm9_boot <- brglm9 %>%
  coeftest(vcovBS(brglm9, type = "wild", cluster =  ~ iso3c))



brglm10 <- lm(fund_doc ~  approval + opp1vote + number_protests +
              previous_fund_doc + duck + partyage + polity2 + left_exec + any_election + minister_is_technocrat_mainstream +
              as.factor(iso3c) + as.factor(quarter) + as.factor(year), 
              data = glm_df)

brglm10_boot <- brglm10 %>%
  coeftest(vcovBS(brglm10, type = "wild", cluster =  ~ iso3c))



brglm11 <- lm(fund_doc ~ approval + opp1vote + number_protests + 
              resource_rents_lag + discovery + real_oil_price_log + gdp_pc_constant_lag + gdp_growth_lag + imf_program + crisis +
              as.factor(iso3c) + as.factor(quarter) + as.factor(year), 
              data = glm_df)

brglm11_boot <- brglm11 %>%
  coeftest(vcovBS(brglm11, type = "wild", cluster =  ~ iso3c))



brglm12 <- lm(fund_doc ~ approval + opp1vote + number_protests + previous_fund_doc + duck + partyage + polity2 + left_exec + 
              any_election + minister_is_technocrat_mainstream +
              resource_rents_lag + discovery + real_oil_price_log + gdp_pc_constant_lag + gdp_growth_lag + imf_program + crisis +
              as.factor(iso3c) + as.factor(quarter) + as.factor(year), 
              data = glm_df)

brglm12_boot <- brglm12 %>%
  coeftest(vcovBS(brglm12, type = "wild", cluster =  ~ iso3c))


# generate tables
stargazer(brglm7_boot,brglm8_boot,brglm9_boot,brglm10_boot,brglm11_boot,brglm12_boot, #type="text",
          omit=c("iso3c","quarter","year"), no.space = T,
          covariate.labels = c("Executive approval (perc)","Opposition vote share (perc)","Number of protests",
                               "Previous fund document = 1","Lame duck = 1", "Party age",
                               "Democracy","Left executive = 1","Election quarter = 1",
                               "Mainstream minister = 1",
                               "Resource rents (perc)","Field discovery = 1","Oil price (USD, log)",
                               "GDP per capita (1,000 USD)","GDP growth (perc)","IMF program = 1", "Crisis = 1"), 
          dep.var.labels = "Fund document")

# retrieve n, aic, ll, etc
stargazer(brglm7,brglm8,brglm9,brglm10,brglm11,brglm12, #type="text",
          omit=c("iso3c","quarter","year"), no.space = T,
          dep.var.labels = "Fund document")

